//
//  OMTBaseViewController.m
//  OMTUIKit
//
//  Created by 昭荣伊 on 2018/7/30.
//

#import "OMTBaseViewController.h"
#import <OMTCoreKit/OMTCoreKit.h>

@interface OMTBaseViewController ()

@end

@implementation OMTBaseViewController

- (void)dealloc {
    NSLog(@"%@ dealloc", NSStringFromClass([self class]));
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.navigationController setNavigationBarHidden:YES];
    [self setupBaseSubviews];
    [self setupBaseConstraints];
}

- (void)setupBaseSubviews {
    self.view.backgroundColor = HexColor(0xF5F5F5);
    [self.view addSubview:self.omt_navigationBar];
    
    // 添加返回按钮
    __weak typeof(self) weakSelf = self;
    self.omt_navigationBar.clickLeftBlock = ^(id sender) {
        __strong typeof(weakSelf) strongSelft = weakSelf;
        [strongSelft backAction:nil];
    };
    self.omt_navigationBar.leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.omt_navigationBar omt_setLeftButtonWithImage:[UIImage imageNamed:@"back"]];
}

- (void)setupBaseConstraints {
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self.view bringSubviewToFront:self.omt_navigationBar];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    if ([self whenViewWillDisappearHideKeyboard]) {
        [self.view endEditing:YES];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if ([self whenTapEmptyHideKeyboard]) {
        [self.view endEditing:YES];
    }
}

- (void)setTitle:(NSString *)title {
    [super setTitle:title];
    self.omt_navigationBar.title = title;
}

#pragma mark - lazyload
- (OMTNavigationBar *)omt_navigationBar {
    if (!_omt_navigationBar) {
        _omt_navigationBar = [OMTNavigationBar navigationBar];
        _omt_navigationBar.layer.shadowOffset = CGSizeMake(0, 5);
        _omt_navigationBar.layer.shadowOpacity = 0.5;
        _omt_navigationBar.layer.shadowRadius = 3;
        _omt_navigationBar.layer.shadowColor = [UIColor colorWithWhite:0 alpha:0.1].CGColor;
        [_omt_navigationBar omt_setBottomLineHidden:YES];
        _omt_navigationBar.clickLeftBlock = ^(id sender) {
            NSLog(@"clickLeftBlock");
        };
        
        _omt_navigationBar.clickRightBlock = ^(id sender) {
            NSLog(@"clickRightBlock");
        };
    }
    return _omt_navigationBar;
}

#pragma mark - Actions
- (void)backAction:(id)sender {
    if (self.willBackBlock) {
        self.willBackBlock(sender);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - OMTAutoHideKeyboardAble
- (BOOL)whenTapEmptyHideKeyboard {
    return YES;
}

- (BOOL)whenViewWillDisappearHideKeyboard {
    return YES;
}

@end

@implementation OMTBaseViewController (Navigation)

- (void)setTitleView:(UIView *)titleView {
    self.omt_navigationBar.titleView = titleView;}

- (UIView *)titleView {
    return self.omt_navigationBar.titleView;
}

@end
